package com.carl.zlds.service.impl;

import com.carl.zlds.tool.util.AssertUtil;
import com.carl.zlds.tool.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carl.zlds.dao.GameTagMapper;
import com.carl.zlds.model.Game;
import com.carl.zlds.model.GameTag;
import com.carl.zlds.model.GameTagInfo;
import com.carl.zlds.pojo.dto.SaveGameTagDTO;
import com.carl.zlds.pojo.vo.GameTagInfoVO;
import com.carl.zlds.pojo.vo.GameTagVO;
import com.carl.zlds.pojo.vo.GameTagWithInfoVO;
import com.carl.zlds.service.IGameService;
import com.carl.zlds.service.IGameTagInfoService;
import com.carl.zlds.service.IGameTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author carl
 */
@Slf4j
@Service
public class GameTagServiceImpl extends ServiceImpl<GameTagMapper, GameTag> implements IGameTagService {

    @Autowired
    private IGameService gameService;

    @Autowired
    private IGameTagInfoService gameTagInfoService;

    @Override
    public void saveGameTag(Long id, SaveGameTagDTO dto) {
        GameTag gameTag;
        String name = dto.getName();
        Long gameId = dto.getGameId();

        Game game = gameService.getOne(new QueryWrapper<Game>().eq("id", gameId).eq("weight", 0));
        AssertUtil.isNull("游戏不存在", game);

        if (id > 0) {
            gameTag = this.baseMapper.selectOne(new QueryWrapper<GameTag>().eq("id", id).eq("weight", 0));
            AssertUtil.isNull("游戏tag不存在", gameTag);
            Wrapper<GameTag> wrapper = new QueryWrapper<GameTag>().eq("name", name)
                    .eq("weight", 0).ne("id", id);
            GameTag gt = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNotNull("游戏tag已存在", gt);
        } else {
            Wrapper<GameTag> wrapper = new QueryWrapper<GameTag>().eq("id", id)
                    .eq("name", name).eq("weight", 0);
            GameTag gt = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNotNull("游戏tag已存在", gt);
            gameTag = new GameTag();
        }

        gameTag.setName(name);
        gameTag.setGameId(gameId);
        gameTag.setSort(dto.getSort());

        if (id > 0) {
            this.baseMapper.updateById(gameTag);
        } else {
            this.baseMapper.insert(gameTag);
        }
    }

    @Override
    public void deleteGameTag(Long id) {
        Wrapper<GameTag> wrapper = new QueryWrapper<GameTag>().eq("id", id).eq("weight", 0);
        GameTag gameTag = this.baseMapper.selectOne(wrapper);
        AssertUtil.isNull("游戏tag不存在", gameTag);

        gameTag.setWeight(-1);
        this.baseMapper.updateById(gameTag);
    }

    @Override
    public List<GameTagVO> getGameTagList(Long gameId) {
        return this.baseMapper.getGameTagList(gameId);
    }

    @Override
    public List<GameTagWithInfoVO> getGameTagListWithTag(Long gameId, Long parentId) {
        List<GameTagVO> gameTagList = this.baseMapper.getGameTagList(gameId);
        List<GameTagWithInfoVO> list = ObjUtil.processModelList(gameTagList, GameTagWithInfoVO.class);
        for (GameTagWithInfoVO item : list) {
            List<GameTagInfo> gameTagInfos = gameTagInfoService.getGameTagInfoList(item.getId(), parentId);
            List<GameTagInfoVO> GameTagInfoVOList = ObjUtil.processModelList(gameTagInfos, GameTagInfoVO.class);
            item.setGameTagInfoList(GameTagInfoVOList);
        }
        return list;
    }
}
